import  tkinter
from tkinter import ttk
import os
class TreeWindows(tkinter.Frame):#作为背景的视图
    #master    传入Frame的主干视图
    def __init__(self,master,path,otherWin):
        frame=tkinter.Frame(master)
        frame.grid(row=0,column=0) #与infoWindows左右分布
        self.otherWin=otherWin


        self.tree=tkinter.ttk.Treeview(frame)
        self.tree.pack(side=tkinter.LEFT,fill=tkinter.Y)

        #splitext将路径的最后一级分离
        # print(os.path.splitext(path))
        tempPath=self.getLastPath(path)
        root=self.tree.insert("","end",text=tempPath,open=True,values=(path))#open 默认是否显示

        self.loadTree(root,path)

    #添加滚动条
        self.sy=tkinter.Scrollbar(frame)
        self.sy.pack(side=tkinter.RIGHT,fill=tkinter.Y)
        self.sy.config(command=self.tree.yview)
        self.tree.config(yscrollcommand=self.sy.set)

     #绑定事件
        self.tree.bind("<<TreeviewSelect>>", self.func)

    def func(self,event):
        self.v=event.widget.selection()# widget 触发这个事件的小构建对象
        for sv in self.v:
            file=self.tree.item(sv)["text"]
            self.otherWin.ev.set(file)
            print(file)
            apath=self.tree.item(sv)["values"][0]
            print(apath)

            (待写)






    #递归方法
    def loadTree(self,parent,parentPath):
        for fileName in os.listdir(parentPath):
            absPath=os.path.join(parentPath,fileName)
            #插入树枝
            tree=self.tree.insert(parent,"end",text=self.getLastPath(absPath),values=(absPath))
            if os.path.isdir(absPath):
                self.loadTree(tree, absPath)


    def getLastPath(self,path):
        pathList=os.path.split(path)
        print(pathList)
        return pathList[-1]